package com.aacar.interceptor;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.alibaba.fastjson.JSON;

@Aspect
@Component
@Order(1)
public class LogRecordAspect {
	
	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Before(value="(execution(* com.aacar.controller..*.*(..))) ")
	public void before(JoinPoint joinPoint) {
		String url = null;
		try {
			url = generateUrl(joinPoint);
		} catch (Exception e) {
			//e.printStackTrace();
		}
		if(url!=null){
			List<Object> objs = new ArrayList<Object>();
			for (Object obj : joinPoint.getArgs()) {
				if (obj instanceof CommonsMultipartFile) {
					CommonsMultipartFile commFile = (CommonsMultipartFile) obj;
					Map map = new HashMap();
					map.put("originalFilename", commFile.getOriginalFilename());
					map.put("name", commFile.getName());
					map.put("size", commFile.getSize());
					objs.add(map);
				} else {
					objs.add(obj);
				}
			}
	
			String jsonString="{}";
			try {
				jsonString = JSON.toJSONString(objs);
			} catch (Exception e) {
				//e.printStackTrace();
			}
			
			logger.info("\n");
			logger.info("##########################################################");
			logger.info("######## request url: " + url);
			logger.info("######## request param: " + jsonString);
			logger.info("##########################################################");
			logger.info("\n");
		}
	}
	
	@AfterReturning(value="(execution(* com.aacar.controller..*.*(..)))",argNames="joinPoint,retValue", returning="retValue")
	public void after(JoinPoint joinPoint,Object retValue) {
		String generateUrlJson =  null;
		try {
		   generateUrlJson = generateUrl(joinPoint);
		} catch (Exception e) {
			//e.printStackTrace();
		}
		String jsonString = JSON.toJSONString(retValue);
		if(jsonString.length()>5000){
			jsonString="jsonString.length:"+jsonString.length();
		}
		if(generateUrlJson!=null){
			logger.info("\n");
			logger.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
			logger.info("@@@@@@@@ response value: " + jsonString);
			logger.info("@@@@@@@@ response (request url): " + generateUrlJson);
			logger.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
		}
	}

	private String generateUrl(JoinPoint joinPoint) {
		String url = "";

		RequestMapping requestMappingMethod = null;
		RequestMapping requestMappingClass = null;

		Class<?> cls = joinPoint.getTarget().getClass();
		requestMappingClass = cls.getAnnotation(RequestMapping.class);
		Method[] methods = cls.getMethods();
		String methodName = joinPoint.getSignature().getName();
		for (Method m : methods) {
			// 获取执行方法前的注解信息。
			if (m.getName().equals(methodName)) {
				requestMappingMethod = m.getAnnotation(RequestMapping.class);
				break;
			}
		}

		String[] valueClasses = requestMappingClass.value();
		String[] valueMethods = requestMappingMethod.value();
		if (valueClasses.length > 0) {
			url = url.concat(valueClasses[0]);
		}
		if (valueMethods.length > 0) {
			url = url.concat(valueMethods[0]);
		}
		return url;
	}

	

}
